﻿Imports System.Data.SqlClient

Public Class frmUserAccProfileUpdate

    Public SNo As Integer

    'Sub FillJobTitles()
    '    Try
    '        Me.Cursor = Cursors.WaitCursor

    '        Dim cmd As New SqlCommand("Select * From JobTitles Order By JobTitle", cnn)
    '        Dim Reader As SqlDataReader

    '        Me.CombJobTitle.Items.Clear()

    '        cnn.Open()
    '        Reader = cmd.ExecuteReader
    '        While Reader.Read
    '            Me.CombJobTitle.Items.Add(Reader.Item("JobTitle"))
    '        End While
    '        cnn.Close()

    '        Me.Cursor = Cursors.Default
    '    Catch ex As Exception
    '        Me.Cursor = Cursors.Default
    '        If cnn.State = ConnectionState.Open Then
    '            cnn.Close()
    '        End If
    '        MsgBox(ex.ToString)
    '    End Try
    'End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Sub FillUserProfile()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From Users Where SNo=" & SNo, cnn)
            Dim Reader As SqlDataReader

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.txtFullName.Text = Reader.Item("FullName")
                Me.txtJobTitle.Text = Reader.Item("JobTitle")
                Me.txtEmail.Text = Reader.Item("Email")
                Me.txtPhoneNo.Text = Reader.Item("PhoneNo")
                Me.CombBranch.SelectedItem = Reader.Item("Branch")
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    'Sub FillJobTitles()
    '    Try
    '        Me.Cursor = Cursors.WaitCursor

    '        Dim cmd As New SqlCommand("Select * From JobTitles Order By JobTitle", cnn)
    '        Dim Reader As SqlDataReader

    '        Me.CombJobTitle.Items.Clear()

    '        cnn.Open()
    '        Reader = cmd.ExecuteReader
    '        While Reader.Read
    '            Dim Item As New ListBox
    '            Me.CombJobTitle.Items.Add(Reader.Item("JobTitle"))
    '        End While
    '        cnn.Close()

    '        Me.Cursor = Cursors.Default
    '    Catch ex As Exception
    '        Me.Cursor = Cursors.Default
    '        If cnn.State = ConnectionState.Open Then
    '            cnn.Close()
    '        End If
    '        MsgBox(ex.ToString)
    '    End Try
    'End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If Me.txtFullName.Text.Length = 0 OrElse Me.txtJobTitle.Text.Trim.Length = 0 OrElse _
             Me.txtEmail.Text.Trim.Length = 0 OrElse Me.txtPhoneNo.Text.Trim.Length = 0 Then
            MsgBox("Please fill in all fields")
            Exit Sub
        ElseIf Me.txtEmail.Text.Trim.Length > 0 And _
                       Not System.Text.RegularExpressions.Regex.IsMatch(Me.txtEmail.Text.Trim, "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$") Then
            MsgBox("Please enter a valid email address or leave blank")
            Exit Sub
        Else
            Try
                Me.Cursor = Cursors.WaitCursor


                Dim cmd As New SqlCommand("Update Users Set FullName=@FullName,JobTitle=@JobTitle,PhoneNo=@PhoneNo," & _
                                          "Email=@Email,Branch=@Branch,UpdateUser=@UpdateUser,UpdateDate=GetDate() " & _
                                          "Where SNo=" & SNo, cnn)
                cnn.Open()

                'Add values
                cmd.Parameters.AddWithValue("@FullName", Me.txtFullName.Text.Trim)
                cmd.Parameters.AddWithValue("@JobTitle", Me.txtJobTitle.Text.Trim)
                cmd.Parameters.AddWithValue("@PhoneNo", Me.txtPhoneNo.Text.Trim)
                cmd.Parameters.AddWithValue("@Email", Me.txtEmail.Text.Trim)
                cmd.Parameters.AddWithValue("@Branch", Me.CombBranch.SelectedItem)
                cmd.Parameters.AddWithValue("@UpdateUser", CurrentUser)

                cmd.ExecuteNonQuery()
                cnn.Close()

                MsgBox("Updated Successfully!", MsgBoxStyle.Information, Me.Text)

                Me.Cursor = Cursors.Default

                Me.Close()
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                MsgBox(ex.ToString)
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
            End Try
        End If
    End Sub

    Sub FillBranches()
        Try
            Me.Cursor = Cursors.WaitCursor

            Me.CombBranch.Items.Clear()
            Dim Branches As ArrayList = GetBranches()

            For Each Branch As String In Branches
                Me.CombBranch.Items.Add(Branch)
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class